package Chapter6.WebTable.ObjectMap;

import Chapter6.WebTable.WebElementExtender;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.io.File;

import static org.junit.Assert.*;

/**
 * Created by m.zabarylo on 10/7/2014.
 */
public class ObjectMapDemo {
    private WebDriver driver;
    private StringBuffer verificationErrors = new StringBuffer();
    private ObjectMap map;

    @Before
    public void setUp() throws Exception
    {
        driver = new ChromeDriver();
        driver.get("http://dl.dropbox.com/u/55228056/bmicalculator.html");
        driver.manage().window().maximize();
    }

    @Test
    public void testBmiCalculator()
    {
        try {
            //get the object map file
            map = new ObjectMap("D:\\ZabaryloMaksym\\IntelliJProjects\\projectZ\\webdriverbasics\\src\\test\\java\\Chapter6\\WebTable\\ObjectMap\\objectmap.properties");

            //Get the Height element
            WebElement height = driver.findElement(map.getLocator("height_field"));
            height.sendKeys("181");

            WebElement weight = driver.findElement(map.getLocator("weight_field"));
            weight.sendKeys("80");

            WebElement calculateButton = driver.findElement(map.getLocator("calculate_button"));
            calculateButton.click();

            //verify the bmi
            WebElement bmi = driver.findElement(map.getLocator("bmi_field"));
            assertEquals("24.4", bmi.getAttribute("value"));
        } catch (Exception e) {
            verificationErrors.append(e.toString());
        }
    }
    @Test
    public void testElementScreenshot()
    {
        WebElement pmoambsdiv = driver.findElement(By.className("pmoabs"));
        try {
            FileUtils.copyFile(WebElementExtender.captureElementBitmap(pmoambsdiv), new File("d:\\ZabaryloMaksym\\div.png"));
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    @After
    public void tearDown() throws Exception
    {
        driver.quit();

        String verificationErrorSting = verificationErrors.toString();
        if (!"".equals(verificationErrorSting)){
            fail(verificationErrorSting);
        }
    }
}
